package com.terry.test2.dp;

import lombok.Data;

import javax.xml.bind.ValidationException;
import java.math.BigDecimal;

/**
 * @author 小八
 * @date 2023年02月01日15:52
 */
@Data
public class ExchangeRate extends DpBase{
    private BigDecimal rate;
    private Currency from;
    private Currency to;

    public ExchangeRate(BigDecimal rate,Currency from,Currency to){
        this.rate = rate;
        this.from = from;
        this.to = to;
    }

    public Money exchange(Money fromMoney) throws ValidationException {
        notNull(fromMoney,"金额不能为空");
        isTrue(this.from.getCurrency().equals(fromMoney.getCurrency()),"货币类型不一致");
        BigDecimal targetAmount = fromMoney.getAmount().multiply(rate);
        return new Money(targetAmount,to);
    }
}
